﻿using System;
using System.Management;

namespace VGSoftware.FileShutdownService
{
	public static class Shutdown
	{
		public static void TurnOff()
		{
			CallWin32(5);
		}

		public static void Restart()
		{
			CallWin32(6);
		}

		private static void CallWin32(int flag)
		{
			ManagementBaseObject shutdownManagementBaseObject = null;
			ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem");
			managementClass.Get();

			managementClass.Scope.Options.EnablePrivileges = true;
			ManagementBaseObject parameters = managementClass.GetMethodParameters("Win32Shutdown");

			parameters["Flags"] = flag;
			parameters["Reserved"] = "0";
			foreach (ManagementObject managementObject in managementClass.GetInstances())
			{
				shutdownManagementBaseObject = managementObject.InvokeMethod("Win32Shutdown", parameters, null);
			}
		}
	}
}